package com.yunwaikeji.gy_tool_library.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.yunwaikeji.gy_tool_library.info.GYMenuViewInfo;
import com.yunwaikeji.gy_tool_library.widget.GYMenuView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYMenuViewUtils implements GYMenuViewInfo {
    public static GYMenuViewUtils getInstance() {
        return new GYMenuViewUtils();
    }

    public boolean setOnLeftEmptyListener(OnEmptyListener onEmptyListener, GYMenuView... gyMenuViews) {
        return setOnLeftEmptyListener(onEmptyListener, Arrays.asList(gyMenuViews));
    }

    public boolean setOnLeftEmptyListener(OnEmptyListener onEmptyListener, List<? extends GYMenuView> gyMenuViewList) {
        return setOnEmptyListener(LeftRight.LEFT, onEmptyListener, gyMenuViewList);
    }

    public boolean setOnRightEmptyListener(OnEmptyListener onEmptyListener, GYMenuView... gyMenuViews) {
        return setOnRightEmptyListener(onEmptyListener, Arrays.asList(gyMenuViews));
    }

    public boolean setOnRightEmptyListener(OnEmptyListener onEmptyListener, List<? extends GYMenuView> gyMenuViewList) {
        return setOnEmptyListener(LeftRight.RIGHT, onEmptyListener, gyMenuViewList);
    }

    public boolean setOnEmptyListener(int leftRight, OnEmptyListener onEmptyListener, GYMenuView... gyMenuViews) {
        return setOnEmptyListener(leftRight, onEmptyListener, Arrays.asList(gyMenuViews));
    }

    public boolean setOnEmptyListener(int leftRight, OnEmptyListener onEmptyListener, List<? extends GYMenuView> gyMenuViewList) {
        boolean isEmpty = false;
        List<GYMenuView> emptyList = new ArrayList<>();
        for (GYMenuView gyMenuView : gyMenuViewList) {
            String string = null;
            switch (leftRight) {
                case LeftRight.LEFT:
                    string = gyMenuView.getLeftTextString();
                    break;
                case LeftRight.RIGHT:
                    string = gyMenuView.getRightTextString();
                    break;
            }
            if (TextUtils.isEmpty(string)) {
                isEmpty = true;
                emptyList.add(gyMenuView);
            }
        }
        if (onEmptyListener != null) {
            onEmptyListener.onEmpty(isEmpty, emptyList);
        }
        return isEmpty;
    }

    public interface OnEmptyListener {
        void onEmpty(boolean isEmpty, List<GYMenuView> emptyList);
    }

    public Builder newBuilder() {
        return new Builder();
    }

    public class Builder {
        private int style = WRAP_CONTENT;

        private int leftTextColor = WRAP_CONTENT;
        private int leftHintColor = WRAP_CONTENT;
        private int leftTextSize = WRAP_CONTENT;
        private Drawable leftDrawable = null;
        private int leftDrawableWidthPx = WRAP_CONTENT;
        private int leftDrawableHeightPx = WRAP_CONTENT;
        private int leftDrawableWidthDp = WRAP_CONTENT;
        private int leftDrawableHeightDp = WRAP_CONTENT;
        private int leftSpacingPx = WRAP_CONTENT;
        private int leftSpacingDp = WRAP_CONTENT;

        private int rightTextColor = WRAP_CONTENT;
        private int rightHintColor = WRAP_CONTENT;
        private int rightTextSize = WRAP_CONTENT;
        private Drawable rightDrawable = null;
        private int rightDrawableWidthPx = WRAP_CONTENT;
        private int rightDrawableHeightPx = WRAP_CONTENT;
        private int rightDrawableWidthDp = WRAP_CONTENT;
        private int rightDrawableHeightDp = WRAP_CONTENT;
        private int rightSpacingPx = WRAP_CONTENT;
        private int rightSpacingDp = WRAP_CONTENT;

        private int spacingPx = WRAP_CONTENT;
        private int spacingDp = WRAP_CONTENT;
        private int backgroundColor = WRAP_CONTENT;
        private int pressColor = WRAP_CONTENT;

        private boolean clickable = true;
        private int inputType = WRAP_CONTENT;

        public Builder setStyle(int style) {
            this.style = style;
            return this;
        }

        public Builder setLeftTextColor(int leftTextColor) {
            this.leftTextColor = leftTextColor;
            return this;
        }

        public Builder setLeftHintColor(int leftHintColor) {
            this.leftHintColor = leftHintColor;
            return this;
        }

        public Builder setLeftTextSize(int leftTextSize) {
            this.leftTextSize = leftTextSize;
            return this;
        }

        public Builder setLeftDrawable(Drawable leftDrawable) {
            this.leftDrawable = leftDrawable;
            return this;
        }

        public Builder setLeftDrawableWidthPx(int leftDrawableWidthPx) {
            this.leftDrawableWidthPx = leftDrawableWidthPx;
            return this;
        }

        public Builder setLeftDrawableHeightPx(int leftDrawableHeightPx) {
            this.leftDrawableHeightPx = leftDrawableHeightPx;
            return this;
        }

        public Builder setLeftDrawableWidthDp(int leftDrawableWidthDp) {
            this.leftDrawableWidthDp = leftDrawableWidthDp;
            return this;
        }

        public Builder setLeftDrawableHeightDp(int leftDrawableHeightDp) {
            this.leftDrawableHeightDp = leftDrawableHeightDp;
            return this;
        }

        public Builder setLeftSpacingPx(int leftSpacingPx) {
            this.leftSpacingPx = leftSpacingPx;
            return this;
        }

        public Builder setLeftSpacingDp(int leftSpacingDp) {
            this.leftSpacingDp = leftSpacingDp;
            return this;
        }

        public Builder setRightTextColor(int rightTextColor) {
            this.rightTextColor = rightTextColor;
            return this;
        }

        public Builder setRightHintColor(int rightHintColor) {
            this.rightHintColor = rightHintColor;
            return this;
        }

        public Builder setRightTextSize(int rightTextSize) {
            this.rightTextSize = rightTextSize;
            return this;
        }

        public Builder setRightDrawable(Drawable rightDrawable) {
            this.rightDrawable = rightDrawable;
            return this;
        }

        public Builder setRightDrawableWidthPx(int rightDrawableWidthPx) {
            this.rightDrawableWidthPx = rightDrawableWidthPx;
            return this;
        }

        public Builder setRightDrawableHeightPx(int rightDrawableHeightPx) {
            this.rightDrawableHeightPx = rightDrawableHeightPx;
            return this;
        }

        public Builder setRightDrawableWidthDp(int rightDrawableWidthDp) {
            this.rightDrawableWidthDp = rightDrawableWidthDp;
            return this;
        }

        public Builder setRightDrawableHeightDp(int rightDrawableHeightDp) {
            this.rightDrawableHeightDp = rightDrawableHeightDp;
            return this;
        }

        public Builder setRightSpacingPx(int rightSpacingPx) {
            this.rightSpacingPx = rightSpacingPx;
            return this;
        }

        public Builder setRightSpacingDp(int rightSpacingDp) {
            this.rightSpacingDp = rightSpacingDp;
            return this;
        }

        public Builder setSpacingPx(int spacingPx) {
            this.spacingPx = spacingPx;
            return this;
        }

        public Builder setSpacingDp(int spacingDp) {
            this.spacingDp = spacingDp;
            return this;
        }

        public Builder setBackgroundColor(int backgroundColor) {
            this.backgroundColor = backgroundColor;
            return this;
        }

        public Builder setPressColor(int pressColor) {
            this.pressColor = pressColor;
            return this;
        }

        public Builder setClickable(boolean clickable) {
            this.clickable = clickable;
            return this;
        }

        public void build(Context context) {
            build(GYUtils.scanForActivity(context).getWindow().getDecorView());
        }

        public void build(View view) {
            build(getList(view));
        }

        public void build(GYMenuView... gyMenuViews) {
            build(Arrays.asList(gyMenuViews));
        }

        public void build(List<? extends GYMenuView> gyMenuViewList) {
            for (GYMenuView gyMenuView : gyMenuViewList) {
                if (style != WRAP_CONTENT) {
                    gyMenuView.setStyle(style);
                }

                if (leftTextColor != WRAP_CONTENT) {
                    gyMenuView.setLeftTextColor(leftTextColor);
                }
                if (leftHintColor != WRAP_CONTENT) {
                    gyMenuView.setLeftHintColor(leftHintColor);
                }
                if (leftTextSize != WRAP_CONTENT) {
                    gyMenuView.setLeftTextSizePx(leftTextSize);
                }
                if (leftDrawable != null) {
                    gyMenuView.setLeftDrawable(leftDrawable);
                }
                if (leftDrawableWidthPx != WRAP_CONTENT) {
                    gyMenuView.setLeftDrawableWidthPx(leftDrawableWidthPx);
                }
                if (leftDrawableHeightPx != WRAP_CONTENT) {
                    gyMenuView.setLeftDrawableHeightPx(leftDrawableHeightPx);
                }
                if (leftDrawableWidthDp != WRAP_CONTENT) {
                    gyMenuView.setLeftDrawableWidthDp(leftDrawableWidthDp);
                }
                if (leftDrawableHeightDp != WRAP_CONTENT) {
                    gyMenuView.setLeftDrawableHeightDp(leftDrawableHeightDp);
                }
                if (leftSpacingPx != WRAP_CONTENT) {
                    gyMenuView.setLeftSpacingPx(leftSpacingPx);
                }
                if (leftSpacingDp != WRAP_CONTENT) {
                    gyMenuView.setLeftSpacingDp(leftSpacingDp);
                }

                if (rightTextColor != WRAP_CONTENT) {
                    gyMenuView.setRightTextColor(rightTextColor);
                }
                if (rightHintColor != WRAP_CONTENT) {
                    gyMenuView.setRightHintColor(rightHintColor);
                }
                if (rightTextSize != WRAP_CONTENT) {
                    gyMenuView.setRightTextSizePx(rightTextSize);
                }
                if (rightDrawable != null) {
                    gyMenuView.setRightDrawable(rightDrawable);
                }
                if (rightDrawableWidthPx != WRAP_CONTENT) {
                    gyMenuView.setRightDrawableWidthPx(rightDrawableWidthPx);
                }
                if (rightDrawableHeightPx != WRAP_CONTENT) {
                    gyMenuView.setRightDrawableHeightPx(rightDrawableHeightPx);
                }
                if (rightDrawableWidthDp != WRAP_CONTENT) {
                    gyMenuView.setRightDrawableWidthDp(rightDrawableWidthDp);
                }
                if (rightDrawableHeightDp != WRAP_CONTENT) {
                    gyMenuView.setRightDrawableHeightDp(rightDrawableHeightDp);
                }
                if (rightSpacingPx != WRAP_CONTENT) {
                    gyMenuView.setRightSpacingPx(rightSpacingPx);
                }
                if (rightSpacingDp != WRAP_CONTENT) {
                    gyMenuView.setRightSpacingDp(rightSpacingDp);
                }

                if (spacingPx != WRAP_CONTENT) {
                    gyMenuView.setSpacingPx(spacingPx);
                }
                if (spacingDp != WRAP_CONTENT) {
                    gyMenuView.setSpacingDp(spacingDp);
                }
                if (backgroundColor != WRAP_CONTENT) {
                    gyMenuView.setBackgroundColor(backgroundColor);
                }
                if (pressColor != WRAP_CONTENT) {
                    gyMenuView.getBackgroundBuilder().setChangeColor(pressColor).build();
                }
                gyMenuView.setClickable(clickable);
                if (inputType!=WRAP_CONTENT){
                    gyMenuView.setInputType(inputType);
                }
            }
        }
    }

    public List<GYMenuView> getList(Context context) {
        return getList(GYUtils.scanForActivity(context).getWindow().getDecorView());
    }

    public List<GYMenuView> getList(View view) {
        List<GYMenuView> gyMenuViewList = new ArrayList<>();
        if (view instanceof GYMenuView) {
            GYMenuView gyMenuView = (GYMenuView) view;
            gyMenuViewList.add(gyMenuView);
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                gyMenuViewList.addAll(getList(viewGroup.getChildAt(i)));
            }
        }
        return gyMenuViewList;
    }
}
